package com.julis.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.bean.ObuTrade_Info;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by juli on 2020/3/3.
 */

public class TradeAdapter extends RecyclerView.Adapter<TradeAdapter.MyTVHolder> {
    /**
     * 数据存储集合
     **/
    public List<ObuTrade_Info> mDataList = new ArrayList<ObuTrade_Info>();

    /**
     * 集合方式添加数据
     *
     * @param collection 集合
     */
    public boolean addItem(Collection<ObuTrade_Info> collection) {
        boolean b = mDataList.addAll(collection);
        notifyDataSetChanged();
        return b;

    }

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
//    private final ArrayList<String> mData;

    public TradeAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;


    }

    /**
     * 清空数据
     */
    public void clear() {
        mDataList.clear();
    }

    @Override
    public TradeAdapter.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TradeAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.tab_item_1_tradelist, parent, false));
    }

    @Override
    public void onBindViewHolder(final TradeAdapter.MyTVHolder holder, int pos) {
        ObuTrade_Info obu_info = mDataList.get(pos);
        String Checktime = mDataList.get(pos).getChecktime();
        String Mac = mDataList.get(pos).getMac();
        String Remarks = mDataList.get(pos).getRemarks();
        String Results = mDataList.get(pos).getResults();
        String Timelong = mDataList.get(pos).getTimelong();
        String Sn = mDataList.get(pos).getSn();

        holder.tv_time.setText("检测时间：" + Checktime);
        holder.tv_obu.setText("MAC号：" + Mac);
        holder.tv_sn.setText("SN号：" + Sn);
        holder.tv_shichang.setText("检测时长：" + Timelong);
        holder.tv_beizhu.setText("备注：" + Remarks);

        if (Results.equals("成功")) {
            holder.tv_chenggong.setText("检测成功");
            holder.tv_chenggong.setTextColor(mContext.getResources().getColor(R.color.ym_green));
        } else {
            holder.tv_chenggong.setText("检测失败");
            holder.tv_chenggong.setTextColor(mContext.getResources().getColor(R.color.red));
        }
//        holder.roundButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String id = mDataList.get(pos).getId();
//
//                mContext.startActivity(new Intent(mContext, Record_Details_Activity.class).putExtra("uuid", id));
//            }
//        });
//        if (mOnItemClickListener != null) {
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int pos = holder.getLayoutPosition();
//                    mOnItemClickListener.onItemClick(holder.itemView, pos);
//                }
//            });
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    int pos = holder.getLayoutPosition();
//                    mOnItemClickListener.onItemLongClick(holder.itemView, pos);
//                    return false;
//                }
//            });
//        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        TextView tv_chenggong, tv_obu, tv_sn, tv_time, tv_shichang, tv_beizhu;
        RoundButton roundButton;
        LinearLayout Revin;

        MyTVHolder(View itemView) {
            super(itemView);
            tv_chenggong = (TextView) itemView.findViewById(R.id.tv_zhanghao);
            tv_obu = (TextView) itemView.findViewById(R.id.tv_obu);
            tv_sn = (TextView) itemView.findViewById(R.id.tv_sn);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_shichang = (TextView) itemView.findViewById(R.id.tv_shichang);
            tv_beizhu = (TextView) itemView.findViewById(R.id.tv_beizhu);
            Revin = (LinearLayout) itemView.findViewById(R.id.Revin);
            roundButton = (RoundButton) itemView.findViewById(R.id.xiangqing);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private TradeAdapter.OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(TradeAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
